import Token from "@/utils/token";
import {
  getMemberInfo,
  getAmountInfo
} from "@/api/user";
import {
  toFixedDigit
} from "@/utils/format"

export default {
  state: {
    token: Token.get(),
    refreshToken: '',
    expiresIn: '',
    expiration: '',
    getBaseInfo: false,
    account: "",
    replaceName: "",
    address: "",
    age: "",
    birthday: "",
    country: '',
    province: "",
    city: "",
    company: "",
    email: "",
    fax: "",
    mobile: "",
    nickname: "",
    postcode: "",
    qq: "",
    region: "",
    sex: "", // 性别 0：男 1: 女
    username: "",
    wechatImg: "",
    wechatNum: "",
    wechatUrl: "",
    domainFixPriceRate: '',
    domainPreDeleteRate: '',
    shoppingCartCount: 0,// 购物车数量
    siteCount: 0,//消息数量

    // 账户信息
    balance: '',
    freeze: '',
    domainCount: 0,
    orderCount: 0,
    bookingCount: 0,
    biddingCount: 0,
    rebate: '',
    isAgent: '', // 我的申请跳转登陆管理系统
    agentAdminUrl: '',// 跳转的登陆管理系统链接
    levelName: ''// 会员等级
  },
  getters: {},
  mutations: {
    setToken(state, data) {
      data && (state.token = data.accessToken);
      data && (state.refreshToken = data.refreshToken);
      data && (state.expiresIn = data.expiresIn);
      data && (state.expiration = data.expiration);
      data.accessToken && Token.set(data);
    },
    setGetBaseInfo(state, data) {
      state.getBaseInfo = data;
    },
    setAccount(state, data) {
      state.account = data;
    },
    setAddress(state, data) {
      state.address = data;
    },
    setAge(state, data) {
      state.age = data;
    },
    setBirthday(state, data) {
      state.birthday = data;
    },
    setProvince(state, data) {
      state.province = data;
    },
    setCountry(state, data) {
      state.country = data;
    },
    setCity(state, data) {
      state.city = data;
    },
    setCompany(state, data) {
      state.company = data;
    },
    setEmail(state, data) {
      state.email = data;
    },
    setFax(state, data) {
      state.fax = data;
    },
    setMobile(state, data) {
      state.mobile = data;
    },
    setNickname(state, data) {
      state.nickname = data;
    },
    setReplaceName(state, data) {
      state.replaceName = data;
    },
    setPostcode(state, data) {
      state.postcode = data;
    },
    setQq(state, data) {
      state.qq = data;
    },
    setRegion(state, data) {
      state.region = data;
    },
    setSex(state, data) {
      state.sex = data;
    },
    setUsername(state, data) {
      state.username = data;
    },
    setWechatImg(state, data) {
      state.wechatImg = data;
    },
    setWechatNum(state, data) {
      state.wechatNum = data;
    },
    setWechatUrl(state, data) {
      state.wechatUrl = data;
    },
    setBalance(state, data) {
      state.balance = data;
    },
    setFreeze(state, data) {
      state.freeze = data;
    },
    setDomainCount(state, data) {
      state.domainCount = data;
    },
    setOrderCount(state, data) {
      state.orderCount = data;
    },
    setBookingCount(state, data) {
      state.bookingCount = data;
    },
    setBiddingCount(state, data) {
      state.biddingCount = data;
    },
    setRebate(state, data) {
      state.rebate = data;
    },
    setDomainFixPriceRate(state, data) {
      state.domainFixPriceRate = data;
    },
    setDomainPreDeleteRate(state, data) {
      state.domainPreDeleteRate = data;
    },
    setIsAgent(state, data) {
      state.isAgent = data;
    },
    setAgentAdminUrl(state, data) {
      state.agentAdminUrl = data;
    },
    setLevelName(state, data) {
      state.levelName = data;
    },
    setShoppingCartCount(state, data) {
      state.shoppingCartCount = data;
    },
    setSiteCount(state, data) {
      state.siteCount = data;
    },
  },
  actions: {
    /**
     * @description 获取用户信息
     */
    async getUserData({
      state,
      commit
    }) {
      if (!state.token) return;
      let res = await getMemberInfo();
      if (res.code == 200) {
        commit("setGetBaseInfo", true);
        commit("setAccount", res.data.account);
        commit("setDomainCount", res.data.domainCount);
        commit("setOrderCount", res.data.orderCount);
        commit("setBookingCount", res.data.bookingCount);
        commit("setBiddingCount", res.data.biddingCount);
        commit("setAddress", res.data.address);
        commit("setAge", res.data.age);
        commit("setBirthday", res.data.birthday);
        commit("setCountry", res.data.country);
        commit("setProvince", res.data.province);
        commit("setCity", res.data.city);
        commit("setCompany", res.data.company);
        commit("setEmail", res.data.email);
        commit("setFax", res.data.fax);
        commit("setMobile", res.data.mobile);
        commit("setNickname", res.data.nickname);
        commit("setReplaceName", res.data.replaceName);
        commit("setPostcode", res.data.postcode);
        commit("setQq", res.data.qq);
        commit("setRegion", res.data.region);
        commit("setSex", res.data.sex);
        commit("setUsername", res.data.username);
        commit("setWechatImg", res.data.wechatImg);
        commit("setWechatNum", res.data.wechatNum);
        commit("setWechatUrl", res.data.wechatUrl);
        commit('setIsAgent', res.data.isAgent);
        commit('setAgentAdminUrl', res.data.agentAdminUrl);
        commit('setLevelName', res.data.levelName);
        commit('setDomainFixPriceRate', res.data.domainFixPriceRate);
        commit('setDomainPreDeleteRate', res.data.domainPreDeleteRate);
        commit('setShoppingCartCount', res.data.shoppingCartCount);
        commit('setSiteCount', res.data.siteCount);
      }
    },
    /**
     * @description 获取用户账户信息
     */
    async getAmountData({
      state,
      commit
    }) {
      if (!state.token) return;
      let res = await getAmountInfo();
      if (res.code == 200) {
        commit("setBalance", toFixedDigit(res.data.balance, 2));
        commit("setFreeze", toFixedDigit(res.data.freeze, 2));
        commit('setRebate', toFixedDigit(res.data.rebate, 2));
      }
    },
    /**
     * @description 退出登录删除用户信息
     */
    removeUserData({
      commit
    }) {
      Token.remove();
      commit("setToken", '');
      commit("setGetBaseInfo", false);
      commit("setAccount", '');
      commit("setAddress", '');
      commit("setAge", '');
      commit("setBirthday", '');
      commit("setCountry", '');
      commit("setProvince", '');
      commit("setCity", '');
      commit("setCompany", '');
      commit("setEmail", '');
      commit("setFax", '');
      commit("setMobile", '');
      commit("setNickname", '');
      commit("setReplaceName", '');
      commit("setPostcode", '');
      commit("setQq", '');
      commit("setRegion", '');
      commit("setSex", '');
      commit("setUsername", '');
      commit("setWechatImg", '');
      commit("setWechatNum", '');
      commit("setWechatUrl", '');
      commit("setBalance", 0);
      commit("setFreeze", 0);
    },
    // /**
    //  * @description 修改用户信息
    //  */
    // async editUserInfo({ commit }, data) {
    //   let res = await editUserInfo(data);
    //   if (res.code == 200) {
    //     commit("setAvatar", data.headImg);
    //     commit("setUserName", data.nikeName);
    //     commit("setMobile", data.mobile);
    //     commit("setEmail", data.email);
    //   }
    //   return res;
    // },
    // /**
    //  * @description 同步未读消息数量
    //  */
    // async syncMessage({ commit }) {
    //   let res = await syncMessage();
    //   if (res.code == 200) {
    //     commit("setUnreadMessageNum", res.data);
    //   }
    // }
  }
};
